
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
   
class Catalog extends CI_Controller {
    public $page_name = 'catalog';
    public $v_config = array(
                array(
                    'field' => 'class',
                    'label' => '分类',
                    'rules' => 'required'
                )
          
            );

    public function __construct(){
        parent::__construct();
        if (!$this->session->userdata('is_login')){
           redirect('/admin/auth/login');
        }
	}
    public function index(){
        $this->load->model('Catalog_model');    
        $data = array(
            'catalogs' => $this->Catalog_model->get_entries()
        );
        $this->load->view('admin/catalog_index',$data);
    }

    public function add(){
        $this->load->library('form_validation');
        $this->form_validation->set_rules($this->v_config);
        if ($this->form_validation->run() == FALSE){
            $this->load->model('Catalog_model');
            $catalog = new stdClass();
            $catalog->class = '';
            $data = array(
                    "catalog" => $catalog,
                    "title" => '添加'
                    );
            $this->load->view('admin/catalog_form',$data);
        }
        else {
            $this->session->set_userdata('is_login', true);//添加
            $data = $this->input->post();
            $this->load->model('Catalog_model');
            $this->Catalog_model->add_one($data);
            redirect('/admin/catalog/index');
        }
    }

    public function edit($id){
        $this->load->library('form_validation');
        $this->form_validation->set_rules($this->v_config);
        $this->load->model('Catalog_model');
        if ($this->form_validation->run() == FALSE){
            $this->load->model('Catalog_model');
            $data = array(
                    "catalog" => $this->Catalog_model->get_one($id),
                    "title" => '修改',
                    );
            $this->load->view('admin/catalog_form',$data);
        }
        else {
            $this->session->set_userdata('is_login', true);
            $data = $this->input->post();
            $this->Catalog_model->edit_one($id,$data);
            redirect('/admin/catalog/index');
        }
    }

	public function del($id){
            $this->load->model('Catalog_model');
            $this->Catalog_model->del_one($id);
            redirect('/admin/catalog/index');
    }

    
}